package com.mbc.rmt.manager;

import java.sql.SQLException;
import java.util.HashMap;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRResultSetDataSource;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.util.JRLoader;

import com.mbc.common.CommonSingleton;
import com.mbc.common.dao.CommonDAOFactory;
import com.mbc.common.dao.LogDAO;
import com.mbc.common.entities.Log;
import com.mbc.common.exception.DataDuplicationException;
import com.mbc.common.manager.AbstractMgr;
import com.mbc.common.utils.KeyGenerator;
import com.mbc.rmt.dao.AgentDAO;
import com.mbc.rmt.dao.RemittanceDAOFactory;
import com.mbc.rmt.data.AgentDataList;
import com.mbc.rmt.entities.Agent;
import com.mbc.rmt.entities.AgentBalanceRpt;
import com.mbc.rmt.entities.AgentLedgerRpt;
import com.mbc.rmt.entities.AgentRpt;


public class AgentMgr extends AbstractMgr{
	
	private AgentDAO agentDAO;
	private LogDAO logDAO;
	
	public AgentMgr(){
		
	}
	public AgentMgr(String sessionId){
		this.sessionId = sessionId;
	}
	public void create(Agent entity) throws Exception {
		openConnection();
		try {
			agentDAO = RemittanceDAOFactory.getAgentDAO(dbManager
					.getConnection());
			entity.setAgent_id(KeyGenerator.generateKey(getConnection()));
			agentDAO.create(entity);
			
			logDAO = CommonDAOFactory.getLogDAO(getConnection());
			Log l_logentity = new Log();
			l_logentity.setTablename("Agent");
			l_logentity.setLogkey(KeyGenerator.generateKey(getConnection()));
			l_logentity.setKey(entity.getAgent_id());
			l_logentity.setUserid(entity.getUserid());
			l_logentity.setLogstatus(Log.__SAVE_DATA);
			logDAO.getData(l_logentity);
			
		} catch (SQLException e) {
			if(DataDuplicationException.UNIQUE_KEY_ERROR_CODE == e.getErrorCode())
				throw new DataDuplicationException(e.getMessage());
			else
				throw e;
		} finally {
			closeConnection();
		}
	}
	public void update(Agent entity) throws Exception {
		openConnection();
		try {
			agentDAO = RemittanceDAOFactory.getAgentDAO(dbManager
					.getConnection());
			agentDAO.update(entity);
			
			logDAO = CommonDAOFactory.getLogDAO(getConnection());
			Log l_logentity = new Log();
			l_logentity.setTablename("Agent");
			l_logentity.setLogkey(KeyGenerator.generateKey(getConnection()));
			l_logentity.setKey(entity.getAgent_id());
			l_logentity.setUserid(entity.getUserid());
			l_logentity.setLogstatus(Log.__UPDATE_DATA);
			logDAO.getData(l_logentity);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}
	public void delete(long key,long userid) throws Exception {
		openConnection();
		try {
			agentDAO = RemittanceDAOFactory.getAgentDAO(dbManager
					.getConnection());
			
			logDAO = CommonDAOFactory.getLogDAO(getConnection());
			Log l_logentity = new Log();
			l_logentity.setTablename("Agent");
			l_logentity.setLogkey(KeyGenerator.generateKey(getConnection()));
			l_logentity.setKey(key);
			l_logentity.setUserid(userid);
			l_logentity.setLogstatus(Log.__DELETE_DATA);
			logDAO.getData(l_logentity);
			
			agentDAO.delete(key);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}
	public Agent findByCode(String code,long branchID) throws Exception {
		openConnection();
		try {
			agentDAO = RemittanceDAOFactory.getAgentDAO(dbManager
					.getConnection());
			return agentDAO.findByCode(code,branchID);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}
	public AgentDataList findAgents(Agent cri) throws Exception {
		openConnection();
		try {
			agentDAO = RemittanceDAOFactory.getAgentDAO(dbManager
					.getConnection());
			return agentDAO.findAgents(cri);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}
	public AgentDataList getAgentList(AgentRpt agentRpt)throws Exception{
		openConnection();
		try {
			agentDAO = RemittanceDAOFactory.getAgentDAO(dbManager.getConnection());
			return agentDAO.getAgentList(agentRpt);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}
	public JasperPrint prepareAgentReport(AgentRpt agentRpt) throws Exception {
		openConnection();
		JasperPrint l_jsPrint = null;
		try {
			agentDAO = RemittanceDAOFactory.getAgentDAO(getConnection());

			JRResultSetDataSource l_jrRS = new JRResultSetDataSource(agentDAO.prepareAgentReport(agentRpt));

			 JasperReport l_jsRpt =(JasperReport)JRLoader.loadObjectFromFile(CommonSingleton.ABSOLUTE_PATH +"reports/Agent.jasper");

			 HashMap<String, Object> l_params = new HashMap<String, Object>();
				l_params.put("companyname", "mbc");
			l_jsPrint = JasperFillManager.fillReport(l_jsRpt, l_params, l_jrRS);

		} catch (JRException je) {
			throw je;
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}

		return l_jsPrint;

	}

	//(5/8/2014)
	public Agent getAgents(Agent agent) throws Exception {
		openConnection();
		try{
			agentDAO = RemittanceDAOFactory.getAgentDAO(dbManager
					.getConnection());
			if(agent.getCurrencyid() == 0)				
				return agentDAO.getAgents(agent.getAgent_code(),agent.getBranch_id());
			else
				return agentDAO.getAgentsByCurrencyID(agent);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}
	
	public JasperPrint prepareAgentBalanceReport(AgentBalanceRpt entity) throws Exception {
		openConnection();
		JasperPrint l_jsPrint = null;
		try {
			agentDAO = RemittanceDAOFactory.getAgentDAO(getConnection());
			JRResultSetDataSource l_jrRS = new JRResultSetDataSource(agentDAO.prepareAgentBalanceReport(entity));
			JasperReport l_jsRpt = null;
			if(entity.getOrderby().equals("currencyid"))
				l_jsRpt =(JasperReport)JRLoader.loadObjectFromFile(CommonSingleton.ABSOLUTE_PATH +"reports/AgentBalanceByCurrencyRpt.jasper");
			else
				l_jsRpt =(JasperReport)JRLoader.loadObjectFromFile(CommonSingleton.ABSOLUTE_PATH +"reports/AgentBalanceRpt.jasper");

			 HashMap<String, Object> l_params = new HashMap<String, Object>();
				l_params.put("companyname", "mbc");
			l_jsPrint = JasperFillManager.fillReport(l_jsRpt, l_params, l_jrRS);

		} catch (JRException je) {
			throw je;
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}

		return l_jsPrint;

	}
	
	public JasperPrint prepareAgentLedgerReport(AgentLedgerRpt entity) throws Exception {
		openConnection();
		JasperPrint l_jsPrint = null;
		try {
			agentDAO = RemittanceDAOFactory.getAgentDAO(getConnection());
			JRResultSetDataSource l_jrRS = new JRResultSetDataSource(agentDAO.prepareAgentLedgerReport(entity));

			 JasperReport l_jsRpt =(JasperReport)JRLoader.loadObjectFromFile(CommonSingleton.ABSOLUTE_PATH +"reports/AgentLedgerRpt.jasper");

			 HashMap<String, Object> l_params = new HashMap<String, Object>();
				l_params.put("companyname", "mbc");
			l_jsPrint = JasperFillManager.fillReport(l_jsRpt, l_params, l_jrRS);

		} catch (JRException je) {
			throw je;
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}

		return l_jsPrint;

	}


}
